/*global Persist: false, window: false, soundManager: false*/
Ext.ns('Ext.ux.state');
Ext.ux.state.PersistStateProvider = Ext.extend(Ext.state.Provider, {
    constructor: function(config) {
        Ext.ux.state.PersistStateProvider.superclass.constructor.call(this, config);
        this.store = new Persist.Store(config.name);
        if (config.defaults) {
            var me = this;
            $.each(config.defaults, function(key, val) {
                if (!Ext.isDefined(me.get(key))) {
                    me.set(key, val.defaultValue);
                }
            });
            this.defaults = config.defaults;
        }
    },
    set: function(name, value) {
        if (typeof value == "undefined" || value === null) {
            return;
        }
        var val = this.encodeValue(value);
        this.store.set(name, val);
        this.fireEvent("statechange", this, name, value);
    },
    get: function(name, defaultValue) {
        var val = null;
        this.store.get(name, function(k,v) {
            if (k) {
                val = v;
            }
        });
        if (val) {
            return this.decodeValue(val);
        }
        return defaultValue;
    },
    clear: function(name) {
        this.store.remove(name);
        this.fireEvent("statechange", this, name, null);
    }
});

Ext.ux.mattgoldspink.subsonic.UserPrefsStore =  new Ext.ux.state.PersistStateProvider({
    name: 'subtunes-user-preferences',
    defaults: {
        'subsonic-api-url': {
            text: "Subsonic instance url",
			description: "Please enter the url for your subsonic instance",
            type: "string",
            defaultValue: undefined,
			makeHandler: function(key, item){
				return function() {
					window.subtunes.makeRequest(window, [], Ext.apply({}, Ext.ux.mattgoldspink.subsonic.LoginDetails), 0);
				};
			}
        },
        'track-grid-batch-size': {
            text: "Track grid batch load size",
			description: "Tweak this value to load rows into the track grids (default 50)",
            type: "number",
            defaultValue: 250,
			makeHandler: function(key, item){
				return function() {
                    Ext.MessageBox.prompt(item.text, item.description, function(btn, text){
                        if (Ext.isNumber(1 * text)) {
                            Ext.ux.mattgoldspink.subsonic.UserPrefsStore.set(key, text);
                            Ext.ux.mattgoldspink.subsonic.FolderTracksStore.rowCountToProcess = text;
                        } else {
                            Ext.MessageBox.alert('Invalid value', text + ' is not valid number');
                        }
                    });
				};
			}
        },
        "auto-expand-indexes": {
            text: "Auto Expand Indexes",
            type: "boolean",
            defaultValue: false
        },
		'use-html5-audio': {
            text: "Use HTML5 Audio",
            type: "boolean",
            defaultValue: false,
			checkHandler: function(item, checked) {
				Ext.ux.mattgoldspink.subsonic.UserPrefsStore.set('use-html5-audio', checked);
				soundManager.useHTML5Audio = checked;
				soundManager.reboot();
			}
        },
		'force-use-html5-for-mp3': {
            text: "Force MP3 playback to use HTML5 Audio (note: some browsers have poor mp3 support)",
            type: "boolean",
            defaultValue: false,
			checkHandler: function(item, checked) {
				Ext.ux.mattgoldspink.subsonic.UserPrefsStore.set('force-use-html5-for-mp3', checked);
				soundManager.html5Test = (checked ? /(probably|maybe)/i : /(probably)/i);
				soundManager.reboot();
			}
        }
    }
});
